<?php

namespace app\common\command;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\market\Acceleration;
use app\admin\model\user\Prizelog;
use app\common\model\User;
use app\common\model\Userchildren;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Rebatemoneytwo extends Command
{
    protected function configure()
    {
        $this->setName('rebatemoneytwo')->setHelp('砸金蛋1-5每隔10分钟')->setDescription('砸金蛋1-5每隔10分钟');
    }

    protected function execute(Input $input, Output $output)
    {
        $prizeLogModel = new Prizelog();
        $data          = $prizeLogModel
                        ->with(['prize'])
                        ->where('day_time', '<', time())
                        ->where('prize_id', '<', 6)
                        ->where('status', '<', 2)
                        ->select();
        if ($data) {
            Db::startTrans();
            try {
                foreach ($data as $k => $v) {
//                    dump($v->id);
//                    $v->day_time = $v->createtime+86400;
//                    $v->save();
                    if (!$v->prize) {
                        $v->status = 2;
                    } else {
                        $userInfo = User::where(['id' => $v->user_id])->find();
                        if ($userInfo) {
                            $scale         = $v->prize->num / 100;
                            $interest      = bcmul($scale, $v->principal);
                            $user_money    = bcadd($interest, $v->principal);
                            $v->status     = 2;
                            $v->has_return = $user_money;
                            if ($user_money > 0) {
                                WalletService::change($v->user_id, 'score', $user_money, 'prize', $v->id,
                                    '抽中'.$v->prize->title.'奖品返利');
                            }
                        } else {
                            $v->status = 2;
                        }
                    }
                    $v->save();
                }
//                exit();
//                echo '执行成功';
                Db::commit();
            } catch (HttpResponseException $e) {
                Db::rollback();
                $data    = $e->getResponse()->getData();
                $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                echo $message;
            }
        }else{
            echo '无数据';
        }
    }
}
